using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace CocaCola_Inventory_System
{
    public partial class Update : Form
    {

        int _id;
        DataTable types;
        DataTable users;
        DataTable detail_types;
        DataTable details;
        DataTable category;
        DataTable condition;
        DataTable depres;
        DataTable BU;
        DataTable Countries;
        DataTable Locations;
        ArrayList delete;
        ArrayList names;
        ArrayList value;
        public search parent;
        public Update()
        {
            InitializeComponent();
        }

        public int id
        {
            set
            {
                _id = value;
            }
        }
        private void Update_Load(object sender, EventArgs e)
        {
            if (((CocaCola_Inventory_System.Forms.Container)this.MdiParent).security < 2)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            retrieve();
            comboBox1.DataSource = types;
            comboBox1.DisplayMember = "name";
            comboBox1.ValueMember = "id";
            comboBox2.DataSource = users;
            comboBox2.DisplayMember = "name";
            comboBox2.ValueMember = "id";
            comboBox4.DataSource = detail_types;
            comboBox4.DisplayMember = "name";
            comboBox4.ValueMember = "id";
            delete = new ArrayList();
            names = new ArrayList();
            value = new ArrayList();


            
        }

        public void retrieve()
        {
            retrieve r = new retrieve(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con);
            types = r.gettypes();
            detail_types = r.getdetail_types();
            users  = r.getusers();
            object [] record = r.getRecordById(_id);
            textBox18.Text = record[10].ToString();
            textBox1.Text = record[1].ToString();
            string cond  = record[11].ToString();
            int depresvalue = Convert.ToInt32(record[14]);
            //MessageBox.Show(comboBox4.Items.Count.ToString());
           
            //comboBox4.
            textBox21.Text = record[12].ToString();
            textBox2.Text = record[2].ToString();
            textBox3.Text = record[3].ToString();
            textBox4.Text = record[4].ToString();
            if (record[5] != DBNull.Value)
                dateTimePicker1.Value = Convert.ToDateTime(record[5].ToString());
            else
                dateTimePicker1.Value = DateTime.Now;


            textBox6.Text = record[6].ToString();
            if (record[7] != DBNull.Value)
            dateTimePicker2.Value = Convert.ToDateTime(record[7].ToString());
            else
            dateTimePicker1.Value = DateTime.Now;

            label1.Text = record[8].ToString();
            label11.Text = record[record.Length-2].ToString();
            textBox22.Text = record[13].ToString();
            details = (DataTable)record[record.Length-1];
            
            for (int i = 0; i < details.Rows.Count; i++)            
                listBox1.Items.Add(details.Rows[i][1].ToString() + " : " + details.Rows[i][2].ToString());
            condition = r.getCondtions();
            depres = r.getdepressiation();
            category = r.getcategory();

            comboBox6.DataSource = condition;
            comboBox6.DisplayMember = "name";
            comboBox6.ValueMember = "name";

            comboBox5.DataSource = category;
            comboBox5.DisplayMember = "name";
            comboBox5.ValueMember = "id";

            comboBox3.DataSource = depres;
            comboBox3.DisplayMember = "name";
            comboBox3.ValueMember = "id";

             for (int i = 0; i < condition.Rows.Count; i++)
                 if (condition.Rows[i][1].ToString() == cond)
                 {
                     comboBox6.SelectedIndex = i;
                     break;
                 }
             for (int i = 0; i < depres.Rows.Count; i++)
                 if (Convert.ToInt32(depres.Rows[i][0]) == depresvalue)
                 {
                     comboBox3.SelectedIndex = i;
                     break;
                 }
            int refcat = Convert.ToInt32(record[6].ToString());
             for (int i = 0; i < category.Rows.Count; i++)
                 if (Convert.ToInt32(category.Rows[i][0]) == refcat)
                 {
                     comboBox5.SelectedIndex = i;
                     break;
                 }

             
             BU = r.getBU();
             Countries = r.getcountries();
             Locations = r.getlocations();
             comboBox8.DataSource = BU;
             comboBox9.DataSource = Countries;
             comboBox10.DataSource = Locations;

             comboBox8.DisplayMember = "name";
             comboBox8.ValueMember = "id";
             comboBox9.DisplayMember = "name";
             comboBox9.ValueMember = "id";
             comboBox8.DisplayMember = "name";
             comboBox8.ValueMember = "id";
             comboBox10.DisplayMember = "name";
             comboBox10.ValueMember = "id";

             int locationid = Convert.ToInt32(record[15].ToString());
             int countryid = 0;
             for (int i = 0; i < Locations.Rows.Count; i++)
                 if (locationid == Convert.ToInt32(Locations.Rows[i][0]))
                 { 
                    countryid = Convert.ToInt32(Locations.Rows[i][5]);
                    break;
                 }

             int BUid = 0;
             for (int i = 0; i < BU.Rows.Count; i++)
                 if (countryid == Convert.ToInt32(Countries.Rows[i][0]))
                 { 
                     BUid = Convert.ToInt32(Countries.Rows[i][3]);
                     break;
                 }

            for(int i = 0 ; i < Locations.Rows.Count;i++)
                if(locationid == Convert.ToInt32(Locations.Rows[i][0]))
                    comboBox10.SelectedIndex = i;

            for(int i = 0 ; i < Countries.Rows.Count;i++)
                if(countryid == Convert.ToInt32(Countries.Rows[i][0]))
                    comboBox9.SelectedIndex = i;

            for(int i = 0 ; i < BU.Rows.Count;i++)
                if(BUid == Convert.ToInt32(BU.Rows[i][0]))
                    comboBox8.SelectedIndex = i;
             //Countries.DefaultView.RowFilter = "id = -1";
             //Locations.DefaultView.RowFilter = "id = -1";
             //comboBox8.SelectedIndex = -1;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (((CocaCola_Inventory_System.Forms.Container)this.MdiParent).security < 2)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            DialogResult res = MessageBox.Show("Are you sure you want to Delete ?", "Confirm Update", MessageBoxButtons.YesNo);
            if (res != DialogResult.Yes)
                return;

            Insert i = new Insert(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con);
            if (i.deleteRecord(_id, ((CocaCola_Inventory_System.Forms.Container)this.MdiParent).id))
            {
                toolStripStatusLabel1.Text = "Record Deleted";
                MessageBox.Show("Record Deleted Successfuly");
            }
            
            parent.Show();
            parent.refresh();
            this.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0 || listBox1.SelectedIndex >= listBox1.Items.Count)
                return;
            if (listBox1.SelectedIndex < details.Rows.Count)
            {
                delete.Add(details.Rows[listBox1.SelectedIndex][0]);
                details.Rows[listBox1.SelectedIndex].Delete();
            }
            else
            {
                names.RemoveAt(listBox1.SelectedIndex - details.Rows.Count);
                value.RemoveAt(listBox1.SelectedIndex - details.Rows.Count);
            }
            listBox1.Items.RemoveAt(listBox1.SelectedIndex);


        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox8.Text == "")
                return;

            if (comboBox4.SelectedIndex < 0 || comboBox4.SelectedIndex >= comboBox4.Items.Count)
                return;
            names.Add(detail_types.Rows[comboBox4.SelectedIndex][0]);


            value.Add(textBox8.Text);
            listBox1.Items.Add(((DataRowView)comboBox4.SelectedItem)[1] + ":" + textBox8.Text);
            //detail_types.Rows[comboBox4.SelectedIndex].Delete();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (((CocaCola_Inventory_System.Forms.Container)this.MdiParent).security < 2)
            {
                MessageBox.Show("Not Authorized");
                return;
            }

            if (textBox1.Text == "")
            {
                MessageBox.Show("Please Enter Item Name");
                return;
            }
            decimal acq;
            try
            {
                acq = Convert.ToDecimal(textBox2.Text);

            }
            catch
            {
                MessageBox.Show("Error, Please Enter a Number for the Acq. Value");
                return;
            }
           
            if (comboBox1.SelectedIndex < 0 || comboBox1.SelectedIndex >= comboBox1.Items.Count)
                return;

            if (comboBox2.SelectedIndex < 0 || comboBox2.SelectedIndex >= comboBox2.Items.Count)
                return;
            if (comboBox3.SelectedIndex < 0 || comboBox3.SelectedIndex >= comboBox3.Items.Count)
                return;
            if (comboBox5.SelectedIndex < 0 || comboBox5.SelectedIndex >= comboBox5.Items.Count)
                return;
            if (comboBox6.SelectedIndex < 0 || comboBox6.SelectedIndex >= comboBox6.Items.Count)
                return;
            if (comboBox8.SelectedIndex < 0 || comboBox8.SelectedIndex >= comboBox8.Items.Count)
                return;
            if (comboBox10.SelectedIndex < 0 || comboBox10.SelectedIndex >= comboBox10.Items.Count)
                return;

            

            
            DialogResult res = MessageBox.Show("Are you sure you want to update ?","Confirm Update",MessageBoxButtons.YesNo);
            if (res != DialogResult.Yes)
                return;

            Insert i = new Insert(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con);
                int type = -1;
                int user = -1;
                //int ad = -1;

                if (((DataRowView)comboBox2.SelectedItem)[1].ToString() != label11.Text&&comboBox2.Visible)                
                    user = Convert.ToInt32(comboBox2.SelectedValue);
                     
                
                if (((DataRowView)comboBox1.SelectedItem)[1].ToString() != label1.Text&&comboBox1.Visible)
                    type = Convert.ToInt32(comboBox1.SelectedValue);

                DateTime endp;
                DateTime startn;
                DateTime endn;
                /*
                if (dateTimePicker3.Visible)
                {
                    endp = dateTimePicker3.Value;
                    startn = dateTimePicker4.Value;
                    endn = dateTimePicker5.Value;
                }
                else
                {
                    endp = null;
                    endn = null;
                    startn = null;
                }
                 */

                if (i.update(_id, textBox22.Text, comboBox6.SelectedValue.ToString(), textBox21.Text, textBox18.Text, textBox1.Text, Convert.ToDecimal(textBox2.Text), 0, 0, dateTimePicker1.Value, Convert.ToInt32(comboBox5.SelectedValue), dateTimePicker2.Value, type, user, -1, Convert.ToInt32(comboBox3.SelectedValue), ((CocaCola_Inventory_System.Forms.Container)this.MdiParent).id, names.ToArray(), value.ToArray(), delete.ToArray(),dateTimePicker3.Value,dateTimePicker4.Value,dateTimePicker5.Value,checkBox1.Checked,((CocaCola_Inventory_System.Forms.Container)this.MdiParent).stockid,Convert.ToInt32(Locations.Rows[comboBox10.SelectedIndex][0])))
                {
                    toolStripStatusLabel1.Text = "Record Updated";
                    MessageBox.Show("Record Updated Successfuly");
                }
                else
                {
                    toolStripStatusLabel1.Text = "Failed to update Record";
                    MessageBox.Show("Record Failed to Update");
                }
                parent.Show();
                parent.refresh();
                this.Dispose();
        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (!comboBox1.Visible)
            {
                comboBox1.Visible = true;
                button4.Text = "Undo";
            }
            else
            {
                comboBox1.Visible = false;
                button4.Text = "Edit";
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (!comboBox2.Visible)
            {
                comboBox2.Visible = true;
                textBox5.Visible = true;
                label18.Visible = true;
                label19.Visible = true;
                label20.Visible = true;
                dateTimePicker3.Visible = true;
                dateTimePicker4.Visible = true;
                dateTimePicker5.Visible = true;
                button6.Text = "Undo";
               // label15.Visible = true;
            }
            else
            {
                comboBox2.Visible = false;
                textBox5.Visible = false;
                button6.Text = "Edit";
                label18.Visible = false;
                label19.Visible = false;
                label20.Visible = false;
                dateTimePicker3.Visible = false;
                dateTimePicker4.Visible = false;
                dateTimePicker5.Visible = false;
               // label15.Visible = false;
            }
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            if (textBox5.Text =="")
                users.DefaultView.RowFilter = "";
            string s = textBox5.Text;
            string query="";
            string [] keys = s.Split(' ');
            foreach (string key in keys)
                if(key!="")
                query += "name like '*"+key+"*' AND ";
            if(query.Length<3)
                return;
            query = query.Substring(0, query.Length - 4);
            users.DefaultView.RowFilter = query;
        }

        private void label34_Click(object sender, EventArgs e)
        {

        }

        private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox8.SelectedIndex >= 0)
                Countries.DefaultView.RowFilter = "buid = " + BU.Rows[comboBox8.SelectedIndex][0].ToString();
            else
            {
                Countries.DefaultView.RowFilter = "id = -1";
                Locations.DefaultView.RowFilter = "id = -1";
            }
        }

        private void comboBox9_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox9.SelectedIndex >= 0)
                Locations.DefaultView.RowFilter = "countryid = " + Countries.Rows[comboBox9.SelectedIndex][0].ToString();
            else
                Locations.DefaultView.RowFilter = "id = -1";
        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString() == "\r" && comboBox2.SelectedIndex > -1)
            {
                dateTimePicker3.Focus();
            }
        }
    }
}